package algorithms.distanceFunctions;

import java.util.Set;

import dataSet.Nop;
import dataSet.Topic;

public class EuclideanDistance {
public static double euclideanDistance(Nop aNop, Nop aCenter) {
		
		
		Set<Topic> topicList1 = aNop.getTopicWeightMap().keySet();
		Set<Topic> topicList2 = aCenter.getTopicWeightMap().keySet();
		Set<Topic> topicList = joinTopicList(topicList1, topicList2);

		Double topicWeightOfNop = 0.0;
		Double topicWeightOfCenter = 0.0;
		Double distanceSquare = 0.0;
		for (Topic aTopic : topicList) {
			topicWeightOfNop = aNop.getTopicWeight(aTopic);
			topicWeightOfCenter = aCenter.getTopicWeight(aTopic);
			distanceSquare += (topicWeightOfNop - topicWeightOfCenter) * (topicWeightOfNop - topicWeightOfCenter);
		}
		return Math.sqrt(distanceSquare);
	}

	private static Set<Topic> joinTopicList(Set<Topic> topicList1, Set<Topic> topicList2) {
		Set<Topic> topicList = topicList1;
		for (Topic topic : topicList2) {
			if (topicList.contains(topic)) {
				continue;
			} else {
				topicList.add(topic);
			}
		}
		return topicList;
	}
}
